package middleware

import (
	"gitee.com/cdsailyj/go-vpn/initialize"
	"github.com/gin-gonic/gin"
)

// Request 中间件
// 中间件执行顺序: 开始 -> a -> b -> handler -> b -> a -> 结束
func Request(c *gin.Context) {
	logger, _ := initialize.GetDebugLogger()
	logger.Println("Request中间件之前")

	c.Header("Access-Control-Allow-Origin", "*")
	c.Header("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE, OPTIONS")
	c.Header("Access-Control-Allow-Headers", "Origin, Content-Type, Authorization")
	if c.Request.Method == "OPTIONS" {
		c.AbortWithStatus(204)
		return
	}

	c.Next()

	//发送request之后

	logger.Printf("Request中间件之后:%#v \n", c.Writer.Status())
}

func Auth(c *gin.Context) {
	logger, _ := initialize.GetDebugLogger()
	logger.Println("Auth中间件之前")
	//如果验证失败，终端请求
	//ctx.JSON(http.StatusOK, gin.H{"code": 400, "message": "请登录"})
	//c.Abort();
	//return

	c.Next()

	logger.Printf("Auth中间件之后:%#v \n", c.Writer.Status())

}
